/**
 * 
 */
package com.fpt.edu.vn.h2tv.mapper;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fpt.edu.vn.h2tv.dao.TbPaymentMethodDAO;
import com.fpt.edu.vn.h2tv.dao.TbShippingMethodDAO;
import com.fpt.edu.vn.h2tv.dao.entity.TbOrder;
import com.fpt.edu.vn.h2tv.dao.entity.TbShippingMethod;
import com.fpt.edu.vn.h2tv.dto.Orders;

/**
 * @author niteha
 *
 */
public class OrderMapper {

	private static final Logger LOGGER = LoggerFactory.getLogger(OrderMapper.class);

	private OrderMapper() {}

	public static Orders toDTO(TbOrder orderPE) {

		if (orderPE == null) {
			return null;
		} else {
			Orders orderDTO = new Orders();
			orderDTO.setOrderDate(toVSTDateString(orderPE.getOrderdate().getTime(), "yyyy/MM/dd HH:mm:ss"));
			orderDTO.setOrderId(orderPE.getOrderid());
			orderDTO.setPaymentMethod(orderPE.getPaymentmethodid());
			orderDTO.setReceiveAddress(orderPE.getReceiveaddress());
			orderDTO.setReceivePhone(orderPE.getReceivephonenumber());
			orderDTO.setReceiverName(orderPE.getReceivepersonname());
			orderDTO.setShippingMethod(orderPE.getShippingmethodid());
			orderDTO.setStatus(orderPE.getStatus());
			orderDTO.setTotalPrice(orderPE.getTotalprice());
			orderDTO.setUserId(orderPE.getTbAccount().getUserid());
			orderDTO.setUserName(orderPE.getTbAccount().getUsername());

			orderDTO.setShippingMethodName(new TbShippingMethodDAO().findById(orderPE.getShippingmethodid()).getShippingmethod());
			orderDTO.setPaymentMethodName(new TbPaymentMethodDAO().findById(orderPE.getPaymentmethodid()).getPaymentmethod());

			return orderDTO;
		}
	}

	public static TbOrder toPE(Orders orderDTO) {

		if (orderDTO == null) {
			return null;
		} else {
			TbOrder orderPE = new TbOrder();
			orderPE.setOrderdate(new Timestamp(toMilisecond(orderDTO.getOrderDate(), "yyyy/MM/dd HH:mm:ss")));
			orderPE.setOrderid(orderDTO.getOrderId());
			orderPE.setPaymentmethodid(orderDTO.getPaymentMethod());
			orderPE.setReceiveaddress(orderDTO.getReceiveAddress());
			orderPE.setReceivepersonname(orderDTO.getReceiverName());
			orderPE.setReceivephonenumber(orderDTO.getReceivePhone());
			orderPE.setShippingmethodid(orderDTO.getShippingMethod());
			orderPE.setStatus(orderDTO.getStatus());
//			orderPE.setTbAccount(tbAccount);
//			orderPE.setTbOrderDetails(tbOrderDetails);
			orderPE.setTotalprice(orderDTO.getTotalPrice());

			return orderPE;
		}
	}

	private static String toVSTDateString(long milliseconds, String pattern) {
		Date date = new Date(milliseconds);
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		sdf.setTimeZone(TimeZone.getTimeZone("VST"));
		return sdf.format(date);
	}

	private static long toMilisecond(String vstDateString, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		sdf.setTimeZone(TimeZone.getTimeZone("VST"));
		try {
			return sdf.parse(vstDateString).getTime();
		} catch (ParseException e) {
			return 0l;
		}
	}

//	public static void main(String[] args) {
//		System.out.println(System.currentTimeMillis());
//		String date = toVSTDateString(System.currentTimeMillis(), "yyyy/MM/dd HH:mm:ss");
//		System.out.println(date);
//		System.out.println(toMilisecond(date, "yyyy/MM/dd HH:mm:ss"));
//	}
}
